package net.ojbk.saishi.service;

import net.ojbk.database.DatabaseConnection;
import net.ojbk.saishi.dao.syMatchDao;
import net.ojbk.saishi.po.syMatch;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import jxl.Sheet;  
import jxl.Workbook;  
import jxl.read.biff.BiffException;  

public class BackstageService {
	public static boolean checkMatchPw(String matchId, String pw) { // 检测 后台密码 是否正确
		if (matchId == null || pw == null || pw.equals("") || matchId.equals(""))
			return false;
		DatabaseConnection dbcon = new DatabaseConnection();
		syMatchDao matchDao = new syMatchDao(dbcon);
		syMatch match = new syMatch();
		match.setMatchId(matchId);
		matchDao.queryOneRecord(match);
		dbcon.close();
		if (match.getPw() != null && match.getPw().equals(pw))
			return true;
		return false;
	}
	
	public static String[][] getExcelInfo(String matchId) { // 获取 StatisticalInfo.xls 信息 成功
		if (matchId == null || matchId.equals("")) return new String[0][0];
		// 获取 userId
		DatabaseConnection dbcon = new DatabaseConnection();
		syMatchDao matchDao = new syMatchDao(dbcon);
		String userId = matchDao.getUserIdByMatchId(matchId);
		dbcon.close();
		String[][] excelInfo = new String[0][0];
		try {
			// 获取 Excel 路径
			String excelPath = MatchBackService.getExcelPath(userId, matchId);
//			String excelPath = "D:\\java\\saishi_shiyi_ojbk\\WebContent\\MATCH-DATA\\76670\\OYLAQZ\\StatisticalInfo.xls";
			File file = new File(excelPath);
			
            // 创建输入流，读取Excel  
            InputStream is = new FileInputStream(file.getAbsolutePath());  
            // jxl提供的Workbook类  
            Workbook wb = Workbook.getWorkbook(is);  
           
            // Excel的页签数量  
            int sheet_size = wb.getNumberOfSheets();  
            for (int index = 0; index < sheet_size; index++) {  
                // 每个页签创建一个Sheet对象  
                Sheet sheet = wb.getSheet(index);  
                // 构造 返回
                excelInfo = new String[sheet.getRows() - 1][sheet.getColumns()];// 第二行开始
                
                // sheet.getRows()返回该页的总行数  
                for (int i = 1; i < sheet.getRows(); i++) {  // 第二行开始
                    // sheet.getColumns()返回该页的总列数  
                    for (int j = 0; j < sheet.getColumns(); j++) {
                        excelInfo[i-1][j] = sheet.getCell(j, i).getContents(); 
                        System.out.print(excelInfo[i-1][j] + "\t");  
                    }
                    System.out.println("");
                }  
            }  
		}  catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (BiffException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }
		
		
		return excelInfo;
	}
	
	public static void main(String[] args) {
	}
}
